/* vim: set ts=2 et sw=2 cindent fo=qroca: */

package com.globant.google.mendoza.command;

import java.util.Map;
import java.util.HashMap;
import java.util.Date;

import com.globant.google.mendoza.MendozaServer;
import com.globant.google.mendoza.MendozaRequest;
import com.globant.google.mendoza.MendozaServerState;

/** Represents a base mendoza server command.
 */
public abstract class MendozaBaseCommand implements MendozaCommand {

  /** The command name.
   */
  private String name;

  /** The command parameters.
   */
  private Map<String, String> requestParametersLog;

  /** The command result.
   */
  private MendozaCommandResult result;

  /** Timestamp of this command.
   */
  private Date timestamp;

  /** MendozaServer.
   */
  private MendozaServer mendozaServer;

  /** The mendoza server request data.
   */
  private MendozaRequest mendozaReq;

  /** The mendoza server state.
   */
  private MendozaServerState mendozaState;

  /** Constructor.
   *
   * @param mendozaRequest The mendoza server request data.
   *
   * @param theMendozaServer The mendoza server.
   *
   * @param theMendozaState The mendoza server state.
   */
  protected MendozaBaseCommand(final MendozaRequest mendozaRequest,
      final MendozaServer theMendozaServer,
      final MendozaServerState theMendozaState) {
    timestamp = new Date();
    mendozaReq = mendozaRequest;
    mendozaServer = theMendozaServer;
    mendozaState = theMendozaState;
  }

  /** Gets the command timestamp.
   *
   * @return Returns the command timestamp.
   */
  public final Date getTimestamp() {
    return timestamp;
  }

  /** Gets the command name.
   *
   * @return Returns the command name.
   */
  public final String getName() {
    return name;
  }

  /** Gets the command parameters.
   *
   * @return Returns the command parameters.
   */
  public final Map<String, String> getRequestParametersLog() {
    if (requestParametersLog == null) {
      requestParametersLog = new HashMap<String, String>();
    }
    return requestParametersLog;
  }

  /** Gets the command result.
   *
   * @return Returns the command result.
   */
  public final MendozaCommandResult getResult() {
    return result;
  }

  /** Gets the Mendoza Server request.
   *
   * @return Returns the Mendoza Server request.
   */
  protected final MendozaRequest getMendozaRequest() {
    return mendozaReq;
  }

  /** Gets the Mendoza Server.
   *
   * @return Returns the Mendoza Server.
   */
  protected final MendozaServer getMendoza() {
    return mendozaServer;
  }

  /** Gets the Mendoza Server state.
   *
   * @return Returns the Mendoza Server state.
   */
  protected final MendozaServerState getMendozaState() {
    return mendozaState;
  }

  /** Sets the command name.
   *
   * @param commandName The command name.
   */
  protected final void setName(final String commandName) {
    name = commandName;
  }

  /** Sets the mendoza server command result.
   *
   * @param theResult The mendoza server command result.
   */
  protected final void setResult(final MendozaCommandResult theResult) {
    result = theResult;
  }

  /** Adds a new request parameter to the log.
   *
   * @param key The key to put into the map.
   *
   * @param value The value to put into the map.
   */
  protected final void logRequestParameter(
      final String key, final String value) {
    getRequestParametersLog().put(key, value);
  }

  /** Adds the command to the mendoza server status.
   *
   * @param command The mendoza command.
   */
  protected final void logCommand(final MendozaBaseCommand command) {
    mendozaServer.getMendozaStatus().addCommand(this);
  }

  /** String representation of the command.
   *
   * @return Returns the command info.
   */
  public final String toString() {
    StringBuffer sf = new StringBuffer();
    sf.append(getTimestamp());
    sf.append(" - ");
    sf.append(getName());
    sf.append(" - ");
    sf.append(getRequestParametersLog());
    sf.append(" - ");
    sf.append(getResult());
    sf.append("\n");
    return sf.toString();
  }
}

